import {
	setToken,
	setRefreshToken,
	getToken,
	getRefreshToken
} from '@/config/storage'
import {
	getUserInfo
} from "@/api/admin/user.js"
import {
	getPage
} from "@/api/workerManagement.js"

const state = () => ({
	mToken: getToken() || "",
	refreshToken: getRefreshToken() || "",
	user: uni.getStorageSync('user') || ''
})
const getters = {

}
const mutations = {
	setToken(state, token) {
		state.mToken = token;
		setToken(token)
	},
	setRefreshToken(state, token) {
		state.refreshToken = token;
		setRefreshToken(token)
	},
	loginout(state, token) {
		uni.clearStorageSync()
		state.mToken = "";
		state.refreshToken = "";
		uni.reLaunch({
			url: "/pagesall/login/login"
		})
	},
	setuser(state, data) {
		uni.setStorageSync('user', data.sysUser)
		state.user = data.sysUser
		console.log(state.user);
	}
}
const actions = {
	async getUserInfo({
		commit,
		state
	}) {
		let UserInfo = await getUserInfo()
		let data = await getPage({
			phoneNumber: UserInfo.data.sysUser.phone
		})
		if (data.data.records.length == 1) {
			UserInfo.data.sysUser.sjdata = data.data.records[0]
		} else {
			uni.$u.toast("未查询到司机信息")
			return Promise.reject("未查询到司机")
		}
		console.log(data);
		// 13054729089
		// phone
		commit("setuser", UserInfo.data)
	}
}
export default {
	state,
	getters,
	mutations,
	actions
}